﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ICSharpCode.SharpZipLib.Checksums;
using System.IO;
using ICSharpCode.SharpZipLib.Zip;

namespace CommonLibrary.Common
{
    public class ZipHelper
    {

        /// <summary>
        ///  加压指定路劲 指定文件
        /// </summary>
        /// <param name="fileName"></param>
        public void CompressDirectory(List<String> fileNames, String fileName)
        {
            String filePath = Path.GetDirectoryName(fileName);
            String rarName = Path.GetFileNameWithoutExtension(fileName);
            fileNames.Add(filePath + "\\" + rarName + ".xml");
            this.CompressDirectory(fileNames.ToArray(), filePath + "\\" + rarName + ".rar");

            // 删除生成的表单文件
            File.Delete(fileName + ".xml");

        }

        /// <summary>
        ///  加压文件
        /// </summary>
        /// <param name="path"></param>
        /// <param name="address"></param>
        public void CompressDirectory(String[] fileNames, String fileName)
        {
            ZipOutputStream zos = new ZipOutputStream(System.IO.File.Create(fileName));
            zos.SetLevel(6);

            // 创建压缩文件
            this.createFile(zos, fileNames, fileName);
            zos.Finish();
            zos.Close();
        }

        /// <summary>
        ///  解压文件 删除压缩文件
        /// </summary>
        /// <param name="path"></param>
        public void DecompressFile(String path)
        {
            String savePath = Path.GetDirectoryName(path);
            savePath = Path.Combine(savePath, Path.GetFileNameWithoutExtension(path));
            DecompressFile(path, savePath);

            File.Delete(path);
        }

        /// <summary>
        ///  解压文件不删除压缩文件
        /// </summary>
        /// <param name="path"></param>
        /// <param name="savePath"></param>
        public void DecompressFile(String path, String savePath)
        {
            ZipInputStream zipInput = new ZipInputStream(System.IO.File.OpenRead(path));

            ZipEntry fileEntry;
            this.createDirect(savePath);

            while ((fileEntry = zipInput.GetNextEntry()) != null)
            {
                String fileName = Path.GetFileName(fileEntry.Name);
                if (fileName != String.Empty)
                {
                    fileName = savePath + "\\" + fileName;
                    FileStream streamWriter = System.IO.File.Create(fileName);
                    Byte[] buffer = new Byte[zipInput.Length];
                    Int32 size = zipInput.Read(buffer, 0, Int32.Parse(zipInput.Length.ToString()));
                    streamWriter.Write(buffer, 0, size);
                    streamWriter.Close();
                }
            }
            zipInput.Close();
        }

        /// <summary>
        ///  复制文件
        /// </summary>
        /// <param name="filePath"></param>
        /// <param name="adress"></param>
        private void CopyFile(String filePath, String adress)
        {
            File.Copy(filePath, adress);
        }

        /// <summary>
        ///  创建文件(用于创建压缩文件)
        /// </summary>
        /// <param name="zos"></param>
        /// <param name="fileNames"></param>
        /// <param name="address"></param>
        private void createFile(ZipOutputStream zos, String[] fileNames, String address)
        {
            Crc32 crc = new Crc32();

            foreach (String name in fileNames)
            {
                FileStream fs = System.IO.File.OpenRead(name);
                Byte[] buffer = new Byte[fs.Length];

                fs.Read(buffer, 0, Convert.ToInt32(fs.Length));

                String file = Path.GetFileName(name);

                ZipEntry entry = new ZipEntry(file);
                crc.Reset();
                crc.Update(buffer);
                entry.Crc = crc.Value;
                zos.PutNextEntry(entry);
                zos.Write(buffer, 0, Convert.ToInt32(fs.Length));
                fs.Close();
            }

        }

        /// <summary>
        ///  创建指定目录
        /// </summary>
        /// <param name="path"></param>
        private void createDirect(String path)
        {
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
        }
    }
}
